import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        version: {
            code: "1.0.0",
            num: 1
        },
        token: null, //身份认证token
        user: {
            uid: 0,
            name: '',
            avatar: '',
            mobile: '',
            is_manager: false,
            is_manager_root: false,
            is_manager_supper: false,
            menu: []
        },
        layout: "default",
        requesting: false,
        inchat: false,
        chat_msg_new: false
    },
    mutations: {
        user: function (state, user) {
            state.user = user;
            if (user === null) {
                state.user = {
                    uid: 0,
                    name: '',
                    avatar: '',
                    mobile: '',
                    is_manager: false,
                    is_manager_root: false,
                    is_manager_supper: false,
                    menu: []
                };

                localStorage.removeItem("user");
            } else {
                user.avatar = user.avatar === null ? '' : user.avatar;
                localStorage.setItem("user", JSON.stringify(user));
            }
        },
        version(state, version) {
            state.version = version;
            if (version === null) {
                localStorage.removeItem("version");
            } else {
                localStorage.setItem("version", JSON.stringify(version));
            }
        },
        token(state, token) {
            state.token = token;
            if (token === null) {
                localStorage.removeItem("token");
            } else {
                localStorage.setItem("token", JSON.stringify(token));
            }
        },
        layout(state, layout) {
            state.layout = layout;
        },
        inchat(state, inchat) {
            state.inchat = inchat;
        },
        chat_msg_new(state, chat_msg_new) {
            state.chat_msg_new = chat_msg_new;
        },
        requesting(state, requesting) {
            state.requesting = requesting;
        }
    },
    actions: {
        user(context, user) {
            context.commit("user", user);
        },
        token(context, token) {
            context.commit("token", token);
        },
        chat_msg_new(state, chat_msg_new) {
            context.commit("chat_msg_new", token);
        },
        version(context, version) {
            context.commit("version", version);
        },
        layout(context, layout) {
            context.commit("layout", layout);
        },
        requesting(context, requesting) {
            context.commit("requesting", requesting);
        },
    },
    getters: {
        uid: state => {
            let uid = 0;
            // 如果没有数据则检查本地存储
            if (state.user.uid) {
                uid = state.user.uid;
            } else {
                let obj = JSON.parse(localStorage.getItem("user"));
                if (obj === null) {
                    return 0;
                } else {
                    state.user = obj;
                    uid = state.user.uid;
                }
            }
            return isNaN(parseInt(uid)) ? 0 : parseInt(uid);
        },
        user: state => {
            // 如果没有数据则检查本地存储
            if (state.user.uid) {
                return state.user;
            } else {
                let obj = JSON.parse(localStorage.getItem("user"));
                if (obj === null) {
                    return state.user;
                } else {
                    state.user = obj;
                    return state.user;
                }
            }
        },
        layout: state => state.layout,
        requesting: state => state.requesting,
        token: state => {
            // 如果没有数据则检查本地存储
            if (state.token) {
                return state.token;
            } else {
                let obj = JSON.parse(localStorage.getItem("token"));
                if (obj !== null) {
                    state.token = obj;
                    return state.token;
                }
            }
        },
        version: state => {
            // 如果没有数据则检查本地存储
            if (state.version) {
                return state.version;
            } else {
                let obj = JSON.parse(localStorage.getItem("version"));
                if (obj !== null) {
                    state.version = obj;
                    return state.version;
                }
            }
        },
        is_manager: state => state.user.is_manager,
        inchat: state => state.inchat,
        chat_msg_new: state => state.chat_msg_new,
        is_supper: state => {
            return state.user.is_manager_root || state.user.is_manager_supper;
        }
    }
});
export default store;
